package csc.fresher.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import csc.fresher.constant.ProjectBankingConstant;
import csc.fresher.dao.CustomerAccountDao;
import csc.fresher.dao.CustomerDao;
import csc.fresher.dao.SavingAccountTransactionDao;
import csc.fresher.dao.SystemUserDao;
import csc.fresher.dao.impl.SavingInterestRateDaoImpl;
import csc.fresher.entity.Customer;
import csc.fresher.entity.CustomerAccount;
import csc.fresher.entity.SavingAccountHasInterestRate;
import csc.fresher.entity.SavingTransaction;
import csc.fresher.entity.SavingTransactionType;
import csc.fresher.factory.CustomerAccountDaoFactory;
import csc.fresher.service.SavingAccountService;

@Service
public class SavingAccountServiceImpl implements SavingAccountService {

	@Autowired
	CustomerAccountDaoFactory customerDaoFactory;
	@Autowired
	CustomerDao customerDao;
	@Autowired
	SystemUserDao systemUserDao;
	@Autowired
	SavingInterestRateDaoImpl savingInterestRateDaoImpl;
	@Autowired
	SavingAccountTransactionDao savingAccountTransactionDao;

	CustomerAccountDao customerAccountDao = null;

	@PostConstruct
	public void init() {
		customerAccountDao = customerDaoFactory
				.getType(ProjectBankingConstant.SAVING_ACCOUNT_TYPE);
	}

	@Override
	public List<CustomerAccount> getListCustomerAccountByIDCardNumber(
			long idCardNumber) {
		List<CustomerAccount> customerAccounts = new ArrayList<CustomerAccount>();

		if (customerAccountDao != null) {
			customerAccounts = customerAccountDao
					.getListCutomerAccountsbyCardNumberID(idCardNumber);
		}
		return customerAccounts;
	}

	@Override
	public CustomerAccount getCustomerAccountByID(long idCardNumber) {
		CustomerAccount customerAccount = null;
		if (customerAccountDao != null) {
			customerAccount = customerAccountDao
					.getCustomerAccountByID(idCardNumber);
		}
		return customerAccount;
	}

	@Override
	public double getCurrentAccountBalance(CustomerAccount customerAccount) {
		
		long accountID = customerAccount.getAccountNumber();
		SavingAccountHasInterestRate savingAccountHasInterestRate = savingInterestRateDaoImpl.getInterestRateByAccountNumberID(accountID);
		if(savingAccountHasInterestRate != null && savingAccountHasInterestRate.getAccountNumber() != 0){
			double balance = customerAccount.getAccountBalance();
			
			SavingTransaction savingTransaction = savingAccountTransactionDao.getLastSavingTransactionIsApproved(accountID);
			
			
			if(savingAccountHasInterestRate.getToDate().getTime() <= savingTransaction.getBeginTime().getTime() && savingTransaction.getSavingTransactionType().equals(SavingTransactionType.WITHDRAW)){
				return balance;
			}
			System.out.println("balance " + balance + " rate " + savingAccountHasInterestRate.getInterestRate().getRate());
			
			return balance + balance * savingAccountHasInterestRate.getInterestRate().getRate() / 100;
			
		}
		
		return 0;
	}

	@Override
	public CustomerAccount getCustomerAccountByAccNumber(long accNumber) {
		// TODO Auto-generated method stub
		CustomerAccount customerAccount = null;

		if (customerAccountDao != null) {
			customerAccount = customerAccountDao
					.getSavingAccountByCustomerID(accNumber);
		}
		return customerAccount;
	}

	@Override
	public Customer getCustomerByCustomerID(long customerID) {
		Customer customer = null;
		if (customerDao != null) {
			customer = customerDao.getCustomerByID(customerID);
		}
		return customer;
	}

	@Override
	public List<CustomerAccount> getAllSavingAccounts() {
		List<CustomerAccount> customerAccounts = new ArrayList<CustomerAccount>();

		if (customerAccountDao != null) {
			
		}
		return customerAccounts;
	}

}
